package com.oa.controller;

import com.oa.pojo.Dept;
import com.oa.service.DeptService;
import com.oa.service.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author wzm
 * @create 2023-11-13 10:23
 * project_name:web2310
 * @Description
 */
@WebServlet("/dept/*")
public class DeptServlet extends BaseServlet {
    private DeptService deptService = new DeptServiceImpl();

    //查询全部
    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          request.setCharacterEncoding("UTF-8");
          response.setContentType("text/html;charset=UTF-8");
          //1.获取请求参数
         //2.封装对象
        //3.调用service层
        List<Dept> all = deptService.findAll();
        //4.存储数据到域对象
        if(all == null){
            request.setAttribute("error","未查询到数据");
            return;
        }

        request.setAttribute("depts",all);

        //5.转发到页面
        request.getRequestDispatcher("/dept_list.jsp").forward(request,response);
    }

    //新增
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String deptNo = request.getParameter("deptNo");
        String deptName = request.getParameter("deptName");
        String deptLocation = request.getParameter("deptLocation");

        Dept dept = new Dept();
        dept.setDeptNo(deptNo);
        dept.setDeptName(deptName);
        dept.setDeptLocation(deptLocation);

        boolean insert = deptService.insert(dept);
        if(!insert){
            //新增成功 跳转查询全部
            //request.getRequestDispatcher("/dept/findAll").forward(request,response);

            //重定向解决重复提交问题
            response.sendRedirect(request.getContextPath()+"/dept/findAll");
        }else{
            request.setAttribute("msg","新增失败");
            request.getRequestDispatcher("/dept_add.jsp").forward(request,response);
        }


    }

    //根据id查询
    protected void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获的参数
        int id = Integer.parseInt(request.getParameter("id"));
        //获取tag
        String tag = request.getParameter("tag");

        //调用service查询
        Dept dept = deptService.findById(id);

        if(dept != null){
            request.setAttribute("dept", dept);
            if(tag.equals("detail")) {
                request.getRequestDispatcher("/dept_detail.jsp").forward(request, response);
            }else if(tag.equals("update")){
                request.getRequestDispatcher("/dept_update.jsp").forward(request, response);
            }
        }else{
            response.getWriter().write("未查到数据");
        }

    }

    //修改
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获取表单数据
        int id =Integer.parseInt(request.getParameter("id")) ;
        String deptNo = request.getParameter("deptNo");
        String deptName = request.getParameter("deptName");
        String deptLocation = request.getParameter("deptLocation");

        Dept dept = new Dept();
        dept.setId(id);
        dept.setDeptNo(deptNo);
        dept.setDeptName(deptName);
        dept.setDeptLocation(deptLocation);

        //调用service层
        boolean update = deptService.update(dept);
        if(update){
           //重新查询
            response.sendRedirect(request.getContextPath()+"/dept/findAll");
        }else{
            request.getSession().setAttribute("msg","修改出现问题");

            request.getRequestDispatcher("/dept_update.jsp").forward(request, response);
        }


    }

    //删除
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        int id = Integer.parseInt(request.getParameter("id"));
        boolean delete = deptService.delete(id);
        if(delete){
            //成功跳转查询查询全部
            response.sendRedirect(request.getContextPath()+"/dept/findAll");
        }{
           response.getWriter().write("删除失败");
        }
    }

}
